<template>
  <div class="login">
    <div class="left">
      <div class="lit">
        <img src="@/assets/img/login_logo.png" alt="" class="t1" />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form :model="form" class="demo-ruleForm" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            v-model="form.phone"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="验证码"
                v-model="form.code"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img
                :src="baseUrl + '/' + codeUrl"
                alt=""
                class="code"
                @click="codeImgClick"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true">
            <span>我已阅读并同意</span>
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link></el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="buttonbtn" @click="toLogin"
            >登录</el-button
          >
          <el-button type="primary" class="buttonbtn" @click="cse"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login_img.png" alt="" />
    <!-- 对话框 -->
    <dialogs ref="dialogs"></dialogs>
  </div>
</template>
<script>
import dialogs from './dialog'
import { setLocal } from '../uilts/locak'
import { btnlogin } from '../api/register'
export default {
  components: {
    dialogs
  },
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL,
      codeUrl: '/captcha?type=login',
      form: {
        phone: '', //	是	string	手机号
        password: '', //	是	string	密码
        code: '', //	是	string	验证码
        isPass: []
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
              let deg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value) || deg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的邮箱或手机号'))
              }
            }
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        isPass: [
          { required: true, message: '请勾选用户协议', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    // alert(process.env.VUE_APP_URL)
  },
  methods: {
    // 登录
    toLogin () {
      this.$refs.form.validate(ult => {
        if (ult) {
          btnlogin(this.form).then(res => {
            console.log('登录', res)
            this.$message.success('登录成功')
            setLocal('token', res.data.data.token)
            this.$router.push('/layout')
          })
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    // 注册
    cse () {
      this.$refs.dialogs.isShow = true
    },
    // 点击图形码刷新
    codeImgClick () {
      this.codeUrl = '/captcha?type=login&assas' + Date.now()
    }
  }
}
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa, #01c6fa);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    // background-color: yellowgreen;
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 30px;
    .lit {
      display: flex;
      align-items: center;
      .t1 {
        width: 22px;
        height: 17px;
        margin-top: 5px;
      }
      .t2 {
        // height: 10px;
        font-size: 16px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        margin-left: 20px;
      }
      .t3 {
        width: 1px;
        height: 22px;
        background: #c7c7c7;
        margin: 0 20px;
      }
      .t4 {
        font-size: 16px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .demo-ruleForm {
      margin-top: 20px;
      .code {
        width: 100%;
        height: 40px;
      }
      .buttonbtn {
        width: 100%;
        margin-bottom: 20px;
      }
      .el-button + .el-button {
        margin-left: 0;
      }
    }
  }
}
</style>
